"""
web操作中有时候会有多个窗口 , 有时候也有网页的嵌套 ( iframe )

多窗口:
    获取窗口的唯一句柄 , 只需要切换句柄就可以在多个页面中灵活操作了 .

    获取当前窗口:
        driver.current_window_handle
    获取所有窗口:
        driver.window_handles
    切换到最后一个窗口:
        driver.switch_to.window(driver.window_handles[-1])
    切换到第一个窗口:
        driver.switch_to.window(driver.window_handles[0])


多iframe:


"""
from selenium.webdriver.common.by import By
from basic.s1_pytest_base import TestBase


class TestMoreWindows(TestBase):

    def test_oo1(self):
        """
        懂车帝首页搜 领克05 , 点击搜索后会打开新的页面 .
        新页面点击 , 查看更多 , 又会打开新页面 .
        最后在最新页面中 , 点击 展示更多 .
        最后切换回第一个窗口 , 搜索 领克03 .
        """
        self.driver.get('https://www.dongchedi.com/')

        ele_search = self.driver.find_element(By.CSS_SELECTOR, '.search-form')
        ele_search.send_keys('领克05')

        ele_go_search = self.driver.find_element(By.CSS_SELECTOR, '.tw-w-64')
        ele_go_search.click()

        window_all = self.driver.window_handles
        self.driver.switch_to.window(window_all[-1])

        ele_more = self.driver.find_element(By.CSS_SELECTOR, '.fold-btn')
        ele_more.click()

        self.driver.switch_to.window(self.driver.window_handles[-1])

        ele_show_more = self.driver.find_element(By.CSS_SELECTOR, '.tw-bg-video-light-gray')
        ele_show_more.click()

        xpath_price = '//*[contains(@title,"性能套装版")]//ancestor::div[contains(@class,"table_normal__LUrBM ")]' \
                      '//following-sibling::div//following-sibling::div//span/span'
        ele_price = self.driver.find_element(By.XPATH, xpath_price)
        """
        element.text
            获取为: ''
            当 element.is_displayed() 为 '' , 或 False 时 , element.text 获取不到值 .
        element.get_attribute('textContent')
            能获取到
        """
        assert ele_price.get_attribute('textContent') == '26.18万'
        print()

        self.driver.switch_to.window(self.driver.window_handles[0])
        ele_search = self.driver.find_element(By.CSS_SELECTOR, '.search-form')
        ele_search.send_keys('领克03')
        ele_go_search = self.driver.find_element(By.CSS_SELECTOR, '.tw-w-64')
        ele_go_search.click()
